﻿namespace Chain.Common.DEncrypt
{
    using System;
    using System.IO;
    using System.Security.Cryptography;
    using System.Text;
    using System.Web.Security;

    public class DESEncrypt
    {
        private static string sKey = "szYiJiaSoft";

        public static string Decrypt(string Text)
        {
            return Decrypt(Text, sKey);
        }

        public static string Decrypt(string Text, string sKey)
        {
            string str = "";
            try
            {
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                int num = Text.Length / 2;
                byte[] buffer = new byte[num];
                for (int i = 0; i < num; i++)
                {
                    int num3 = Convert.ToInt32(Text.Substring(i * 2, 2), 0x10);
                    buffer[i] = (byte) num3;
                }
                provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                str = Encoding.Default.GetString(stream.ToArray());
            }
            catch
            {
            }
            return str;
        }

        public static string Encrypt(string Text)
        {
            return Encrypt(Text, sKey);
        }

        public static string Encrypt(string Text, string sKey)
        {
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            byte[] bytes = Encoding.Default.GetBytes(Text);
            provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (byte num in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", num);
            }
            return builder.ToString();
        }

        public static string MD5(string ConvertString)
        {
            MD5CryptoServiceProvider provider = new MD5CryptoServiceProvider();
            return BitConverter.ToString(provider.ComputeHash(Encoding.Default.GetBytes(ConvertString)), 4, 8).Replace("-", "");
        }

        public static string MD5_SMS(string source)
        {
            try
            {
                System.Security.Cryptography.MD5 md = new MD5CryptoServiceProvider();
                return BitConverter.ToString(md.ComputeHash(Encoding.UTF8.GetBytes(source))).Replace("-", "");
            }
            catch (Exception)
            {
                return "0";
            }
        }
    }
}

